package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.dao.ReviewInformationDao;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.ReviewInformation;
import com.pgytesting.secondhandcar.service.ExamineInformationService;
import com.pgytesting.secondhandcar.service.LoanRepaymentInformationService;
import com.pgytesting.secondhandcar.service.OrderNameService;
import com.pgytesting.secondhandcar.service.ReviewInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 审核信息(ReviewInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Service("reviewInformationService")
public class ReviewInformationServiceImpl implements ReviewInformationService {
    @Resource
    private ReviewInformationDao reviewInformationDao;
    @Autowired
    private ExamineInformationService examineInformationService;
    @Autowired
    private OrderNameService orderNameService;
    @Autowired
    private OrderNameDao orderNameDao;
    @Autowired
    private LoanRepaymentInformationService loanRepaymentInformationService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ReviewInformation queryById(String id) {
        return this.reviewInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ReviewInformation> queryAllByLimit(int offset, int limit) {
        return this.reviewInformationDao.queryAllByLimit(offset, limit);
    }
    
    /**
     * 条件查询多条数据
     *
     * @param reviewInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<ReviewInformation> queryAll(ReviewInformation reviewInformation) {
        return this.reviewInformationDao.queryAll(reviewInformation);
    }

    /**
     * 新增数据
     *
     * @param reviewInformation 实例对象
     * @return 实例对象
     */
    @Override
    public ReviewInformation insert(ReviewInformation reviewInformation) {
        this.reviewInformationDao.insert(reviewInformation);
        return reviewInformation;
    }

    /**
     * 修改数据
     *
     * @param reviewInformation 实例对象
     * @return 实例对象
     */
    @Override
    public ReviewInformation update(ReviewInformation reviewInformation) {
        this.reviewInformationDao.update(reviewInformation);
        return this.queryById(reviewInformation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.reviewInformationDao.deleteById(id) > 0;
    }

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseMessage riskControlPreliminaryExamination(String id,Integer status,Integer state) {
		OrderName on = orderNameService.queryById(id);
		if(on == null) {
			return ResponseMessage.errorResponse("未查询到对应的订单");
		}
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		if(on.getReviewInformationId() != null) {
			ri.setId(on.getReviewInformationId());
			ri.setWindControlReview(examineId.toString());
			ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			ri.setApprovalStatus(status.toString());
			
			reviewInformationDao.update(ri);
			
			on.setOrderStatus(status);
			on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			orderNameService.update(on);
		}else {
			ri.setId(UtilMethod.getUniqueCode());
			ri.setUserNumber(on.getUserNumber());
			ri.setApprovalStatus(status.toString());
			ri.setWindControlReview(examineId.toString());
			ri.setCreatedBy(SessionUtil.getSessionAdmin().getId().toString());
			ri.setCreatedTime(UtilMethod.unixLong());
			ri.setStatus(0);
			
			reviewInformationDao.insert(ri);
			
			on.setOrderStatus(status);
			on.setReviewInformationId(ri.getId());
			on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			orderNameService.update(on);
		}
		
		if(state.equals(0)) {
			return ResponseMessage.successResponse("风控审核通过成功");
		}else if(state.equals(1)) {
			return ResponseMessage.successResponse("风控审核拒绝成功");
		}else if(state.equals(3)) {
			return ResponseMessage.successResponse("风控审核补件成功");
		}else if(state.equals(5)) {
			return ResponseMessage.successResponse("风控审核共借成功");
		}else if(state.equals(6)) {
			return ResponseMessage.successResponse("风控审核取消共借成功");
		}else {
			return ResponseMessage.successResponse("风控审核其他成功");
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseMessage riskControlFinalJudgment(String id,Integer status,Integer state) {
		OrderName on = orderNameService.queryById(id);
		if(on == null) {
			return ResponseMessage.errorResponse("未查询到对应的订单");
		}
		if(on.getReviewInformationId() == null) {
			return ResponseMessage.errorResponse("风控复审未进行，不能进行终审");
		}
		
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		ri.setId(on.getReviewInformationId());
		ri.setWindControlFinalJudgment(examineId.toString());
		ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		ri.setApprovalStatus(status.toString());
		
		reviewInformationDao.update(ri);
		
		on.setOrderStatus(status);
		on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		orderNameService.update(on);
		
		if(state.equals(0)) {
			return ResponseMessage.successResponse("风控终审通过成功");
		}else if(state.equals(1)) {
			return ResponseMessage.successResponse("风控终审拒绝成功");
		}else{
			return ResponseMessage.successResponse("风控终审驳回成功");
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseMessage operatorsExamineContract(String id,Integer status,Integer state) {
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		ri.setId(id);
		ri.setOperatorContractMaterialReview(examineId.toString());
		ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		ri.setApprovalStatus(status.toString());
		
		reviewInformationDao.update(ri);
		
		OrderName on = orderNameDao.queryReviewInfID(id);
		on.setOrderStatus(status);
		on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		orderNameService.update(on);
		
		if(status.equals(26003)) {
			return ResponseMessage.successResponse("合同初审退回到重新上传成功");
		}else if(status.equals(26004)) {
			return ResponseMessage.successResponse("合同初审拒绝成功");
		}else if(status.equals(26005)) {
			return ResponseMessage.successResponse("合同初审退回到风控成功");
		}else if(status.equals(26006)) {
			return ResponseMessage.successResponse("合同初审补件成功");
		}else{
			return ResponseMessage.successResponse("合同初审通过成功");
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseMessage operatorsReviewContract(String id,Integer status,Integer state) {
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		ri.setId(id);
		ri.setContractMaterialReview(examineId.toString());
		ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		ri.setApprovalStatus(status.toString());
		
		reviewInformationDao.update(ri);
		
		OrderName on = orderNameDao.queryReviewInfID(id);
		on.setOrderStatus(status);
		on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		orderNameService.update(on);
		
		if(status.equals(26008)) {
			return ResponseMessage.successResponse("合同复审退回成功");
		}else {
			return ResponseMessage.successResponse("合同复审通过成功");
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseMessage operatorsExamineLoanMaterial(String id,Integer status,Integer state) {
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		ri.setId(id);
		ri.setOperatorLendingMaterialReview(examineId.toString());
		ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		ri.setApprovalStatus(status.toString());
		
		reviewInformationDao.update(ri);
		
		OrderName on = orderNameDao.queryReviewInfID(id);
		on.setOrderStatus(status);
		on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		orderNameService.update(on);
		
		if(status.equals(27003)) {
			return ResponseMessage.successResponse("放款材料初审驳回成功");
		}else if(status.equals(27004)) {
			return ResponseMessage.successResponse("放款材料初审拒绝成功");
		}else if(status.equals(27005)) {
			return ResponseMessage.successResponse("放款材料初审发起垫付成功");
		}else if(status.equals(27006)) {
			return ResponseMessage.successResponse("放款材料初审退回风控重审成功");
		}else if(status.equals(27007)) {
			return ResponseMessage.successResponse("放款材料初审补件成功");
		}else{
			return ResponseMessage.successResponse("放款材料初审通过成功");
		}
	}

	@Override
	public ResponseMessage operatorsReviewLoanMaterial(String id,Integer status,Integer state) {
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		ri.setId(id);
		ri.setLendingMaterialReview(examineId.toString());
		ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		ri.setApprovalStatus(status.toString());
		
		reviewInformationDao.update(ri);
		
		OrderName on = orderNameDao.queryReviewInfID(id);
		on.setOrderStatus(status);
		on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		orderNameService.update(on);
		
		if(status.equals(27009)) {
			return ResponseMessage.successResponse("放款材料复审驳回成功");
		}else {
			return ResponseMessage.successResponse("放款材料复审通过成功");
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseMessage managementExamineLoanMaterial(String id,Integer status,Integer state) {
		Integer examineId = examineInformationService.insert(state);
		ReviewInformation ri = new ReviewInformation();
		ri.setId(id);
		ri.setQualificationEndReview(examineId.toString());
		ri.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		ri.setApprovalStatus(status.toString());
		
		reviewInformationDao.update(ri);
		
		OrderName on = orderNameDao.queryReviewInfID(id);
		on.setOrderStatus(status);
		on.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		orderNameService.update(on);
		
		if(status.equals(27011)) {
			return ResponseMessage.successResponse("资方终审放款材料驳回成功");
		}else {
			loanRepaymentInformationService.update(id);
			return ResponseMessage.successResponse("资方终审放款材料通过成功");
		}
	}

	@Override
	public List<Map<String,Object>> queryExamineContract() {
		ReviewInformation reviewInformation = new ReviewInformation();
		reviewInformation.setApprovalStatus(Parameters.ORDER_26002.toString()+","+Parameters.ORDER_26007.toString());
		reviewInformation.setStatus(0);
		return reviewInformationDao.queryExamineContract(reviewInformation);
	}

	@Override
	public List<Map<String,Object>> queryExamineLoanMaterial() {
		ReviewInformation reviewInformation = new ReviewInformation();
		reviewInformation.setApprovalStatus(Parameters.ORDER_27002.toString()+","+Parameters.ORDER_27008.toString()+","+Parameters.ORDER_27010.toString());
		reviewInformation.setStatus(0);
		return reviewInformationDao.queryExamineLoanMaterial(reviewInformation);
	}
}